package com.zlq.controller;

import java.io.File;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import sun.applet.Main;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.logging.Logger;

@Controller
//@RequestMapping("file")
public class FileUpload {

    public static final Logger logger=Logger.getLogger(Main.class.toString());


    @PostMapping("/upload1")
    @ResponseBody
    public String onfile(MultipartFile upload) throws IOException {
        File file1 =new File("F:/fileupload/"+upload.getOriginalFilename());//创建file对象
        if(!file1.exists())
            file1.createNewFile();//在磁盘创建该文件
        upload.transferTo(file1);//将接受的文件存储
        return "success";
    }
    @PostMapping("upload2")
    @ResponseBody
    public String onfiles2(MultipartFile upload[]) throws IOException {
        for(int i=0;i<upload.length;i++)
        {
            if(!upload[i].isEmpty())//文件不空
            {
                File imgfile =new File("F:/fileupload/"+upload[i].getOriginalFilename());
                imgfile.createNewFile();
                upload[i].transferTo(imgfile);

                logger.info(upload[i].getOriginalFilename());
            }
        }
        return "success";
    }


}
